<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\ordersIp;


Class OrdersIpModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function findAll()
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findAll();
        return $ip;
    }

    public function findByPK($ip_id)
    {
        $ip = $this->em->find('BillingBundle:ordersIp', $ip_id);
        return $ip;
    }

    public function findByGroupId($group_id)
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findBy(array('group_id' => $group_id));
        return $ip;
    }
    
    public function findByName($name)
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findOneBy(array('name' => $name));
        return $ip;
    }
    
    public function findByMac($mac)
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findOneBy(array('mac' => $mac));
        return $ip;
    }
    
    public function findByAccountAndType($account, $type_id)
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findBy(array('account' => $account, 'type_id' => $type_id));
        return $ip;
    }
    
    public function findByIp($ip)
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findOneBy(array('ip' => $ip));
        return $ip;
    }
    
    public function findFree($group_id)
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findBy(array('group_id' => $group_id, 'type_id' => '0'));
        return $ip;
    }

    public function findNotFree($group_id)
    {
        $query = $this->em->createQuery("select i from BillingBundle:ordersIp i where i.group_id = ".$group_id." and i.type_id <> 0");
        $ip = $query->getResult();
        return $ip;
    }

    public function findAllNotActive()
    {
        $ip = $this->em->getRepository('BillingBundle:ordersIp')->findBy(array('is_active' => "0"));
        return $ip;
    }
    
    public function create($oEntity)
    {    
        $this->em->persist($oEntity);
        $this->em->flush();
    }
    
    public function removeByPK($ip_id)
    {
        $ip = $this->em->find('BillingBundle:ordersIp', $ip_id);
        $this->em->remove($ip);
        $this->em->flush();
    }
}
?>
